<#include "../_inc/_layout.html"/> 
<#macro script>


var data={
    url:'',
    alt:''
};

function initTinymce(){
tinymce.init({
        selector: '#textarea',
        height: 500,
        language: 'zh_CN',
        menubar: false,
        automatic_uploads: true,
        paste_data_images: true,
        convert_urls: false,
        relative_urls : false,
        imagetools_toolbar: "rotateleft rotateright | flipv fliph | editimage imageoptions",
        imagetools_proxy: '${CPATH}/admin/tinymce/image/proxy',
        images_upload_url: '${CPATH}/admin/tinymce/image/upload',
        wordcount_countregex: /[\u4e00-\u9fa5_a-zA-Z0-9]/g,
		file_picker_callback: function(callback, value, meta) {
		 	layer.open({
			    type: 2,
			    title: '选择图片',
			    shadeClose: true,
			    shade: 0.8,
			    area: ['92%', '90%'],
			    content: '${CPATH}/admin/attachment/choose_layer',
			    end:function(){
			    	if(''!=data.url && null != data.url){
			    		callback(data.url, {alt: data.alt});
			    	}
			    }
			}); 
		 },
        plugins: [
		    "advlist autolink autosave link image media imagetools lists charmap print preview hr anchor pagebreak spellchecker",
		    "searchreplace wordcount visualblocks visualchars code codesample fullscreen insertdatetime media nonbreaking",
		    "table contextmenu directionality emoticons template textcolor paste fullpage textcolor colorpicker textpattern"
		  ],
        toolbar1: '  bold italic underline strikethrough removeformat | blockquote hr table image media codesample | anchor link   unlink | alignleft aligncenter alignright alignjustify | bullist numlist     ',
        toolbar2: '  formatselect | outdent indent | forecolor backcolor  |  undo redo | code  fullscreen',
        });
}

var simplemde ;
function initMarkdownEditor(){
	 simplemde = new SimpleMDE({ element: $("#textarea")[0] });
	// simplemde.toTextArea();
}

var _editor = "${_editor!}";
if("tinymce" == _editor){
 		initTinymce();
 	}else{
 		initMarkdownEditor();
}
    
function save(){
	//simplemde.toTextArea();
	//alert(simplemde.markdown(simplemde.value()));
	//alert($("#textarea").text());
	//simplemde = null;
	
	  $('#content_slug').attr('value',$("#slug_text").text());
	
	if("tinymce" == _editor){
		tinymce.activeEditor.uploadImages(function(success) {
			tinymce.triggerSave();
	 		doSubmit();
		});
	}else{
		$("#textarea").text(simplemde.markdown(simplemde.value()));
		doSubmit();
	}
	return false;
 }
 
 function saveAsDraft(){
 	$("#content_status").attr("value","draft");
 	save();
 }
 
 function doSubmit(){
 	$("#form").ajaxSubmit({
			type : "post", 
			dataType : "json", 
			success : function(data) { 
				if(data.errorCode == 0){
					$("#content_id").attr("value",data.data);
					toastr.success('保存成功！','操作成功');
				}else{
					toastr.error(data.message,'操作失败');
				}
			},
			error : function() {
				alert("信息提交错误");
			}
	});
 }
 
function doSelectThumbnail(){
 	layer.open({
			    type: 2,
			    title: '选择图片',
			    shadeClose: true,
			    shade: 0.8,
			    area: ['92%', '90%'],
			    content: '${CPATH}/admin/attachment/choose_layer',
			    end:function(){
			    	if(''!=data.url && null != data.url){
			    		$("#thumbnail").attr("src",data.url);
			    		$("#content_thumbnail").attr("value",data.url);
			    	}
			    }
			}); 
 }
 
 function doRemoveThumbnail(){
 	$("#thumbnail").attr("src","${CPATH}/static/jpress/admin/image/nothumbnail.jpg");
 	$("#content_thumbnail").val("");
 }
 
 function change_editer(){
 	if("tinymce" == _editor){
 		doAjax('${CPATH}/admin/content/changeEditor/markdown');
 	}else{
 		doAjax('${CPATH}/admin/content/changeEditor/tinymce');
 	}
 }
 
function doAjax(url){
	  $.get(url, function(result){
			if(result.errorCode > 0){
				toastr.error(result.message,'操作失败');
			}else{
				location.reload();
			}
	  }); 
  }
 
<#if module ??> 
<#list module.taxonomyTypes! as taxonomyType > 
 $('#_${taxonomyType.name}').tagEditor();
</#list>
</#if>


$("#title").keyup(function(){
	if($('#content_slug').val() == ""){
	   $("#slug_text").text(this.value);
       $('#slug_text').editable('setValue',this.value);
	}
});

$(document).ready(function(){
	
	 $.fn.editable.defaults.mode = 'inline';
	 $('#slug_text').editable();
	 
	 var url = window.location.protocol  +"//"+ window.location.host+"${CPATH}${urlPreffix}" ;
     $("#url_preffix").text(url);
     $('#slug_text').editable('setValue'," ${(content.slug)!'标题'}");
     
     $('#titleurl').on('save', function(e, params) {
    	 $('#content_slug').attr('value',params.newValue);
	 });
});

</#macro> 
<#macro script_import>
<script src="${CPATH}/static/tinymce/tinymce.min.js"></script>
<script src="${CPATH}/static/plugins/tag/jquery.caret.min.js"></script>
<script src="${CPATH}/static/plugins/tag/jquery.tag-editor.min.js"></script>
<script src="${CPATH}/static/plugins/editable/bootstrap-editable.min.js"></script>
<script src="${CPATH}/static/simplemde/simplemde.min.js"></script>
</#macro>
<#macro css_import>
<link rel="stylesheet" href="${CPATH}/static/plugins/tag/jquery.tag-editor.css">
<link rel="stylesheet" href="${CPATH}/static/plugins/editable/bootstrap-editable.css">
<link rel="stylesheet" href="${CPATH}/static/simplemde/simplemde.min.css">
</#macro>
<#macro css>
.editable-input {
	width: 100%;
}
.form-inline .form-control {
    display: inline-block;
    width: 100%;
    vertical-align: middle;
}
</#macro>
<@layout active_id=p child_active_id=c>
<!-- Content Header (Page header) -->
<section class="content-header">
	<h1>${(module.addTitle)!}</h1>
</section>
<!-- Main content -->
<section class="content" style="z-index: 9999">
	<form action="${CPATH}/admin/content/save" id="form" method="post">
		<input type="hidden" name="content.module" value="${(content.module)!m!}"> 
		<input type="hidden" id="content_id" name="content.id" value="${(content.id)!}"> 
		<input type="hidden" name="content.status" value="normal" id="content_status"> 
		<input type="hidden" name="content.created" value="${(content.created)!}" > 
		<input type="hidden" id="content_slug" name="content.slug" value="${(content.slug)!}" > 
		<input type="hidden" name="ucode" value="${ucode}">
		<div class="row">
			<!-- /.col -->
			<div class="col-md-9">
				<div class="form-group">
					<label class="sr-only"></label> <input name="content.title" class="form-control input-lg" id="title" value="${(content.title)!}" type="text" placeholder="在此输入标题">
				</div>
					<div class="pull-right"><a href="javascript:void(0);" onclick="change_editer()">切换编辑器 </a> </div>
					<label>
					<div ${slugDisplay!} >
						网址：<span id="url_preffix"></span><span id="slug_text">${(content.slug)!}</span>${(urlSuffix)!}
					</div>
					</label>
					<div class="box box-solid">
						<div  class="box-body no-padding">
						<textarea id="textarea"  name="content.text" >${(content.text)!}</textarea>
					</div>
					</div>
					<#include "_props_box.html" /> 
			</div>
			<!-- /.col -->
			<div class="col-md-3">
			<#include "_action_box.html" /> 
			<#if module ??> 
				<#list module.taxonomyTypes! as taxonomyType > 
					<#if taxonomyType.formType == "select"> 
						<#include "_select_box.html" />
					<#else> 
						<#include "_input_box.html" /> 
					</#if> 
				</#list>
			</#if> 
			<#include "_thumbnail_box.html" />
			</div>
		</div>
		<!-- /.row -->
	</form>
</section>

</@layout>






